<?php
class flashBuyController extends base
{
    public function __construct($type='api')
    {
        parent::__construct($type);
    }

    /**
     * 闪购商品提醒
     */
    public function doflashRemind() {
        $uid = $this->uid;
        if (!$uid) {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $flashId = common::request('flashId', 'P', 0);
        if (!$flashId || !is_numeric($flashId)) {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $cid = $this->cid;
        $this->result['result']['is_success'] = 0;
        if ($this->load('flashBuy')->flashRemind($uid, $cid, $flashId)) {
            $this->result['result']['is_success'] = 1;
        }
    }

    /**
     * 取消提醒
     */
    public function dodisFlashRemind() {
        $uid = $this->uid;
        if (!$uid) {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $flashId = common::request('flashId', 'P', 0);
        if (!$flashId || !is_numeric($flashId)) {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['is_success'] = 0;
        if ($this->load('flashBuy')->disFlashRemind($uid, $flashId)) {
            $this->result['result']['is_success'] = 1;
        }
    }

    /**
     * 查询是否开启提醒(用户是否开启提醒+ 闪购商品是否开始状态)
     */
    public function dogetRemindStatus() {
        $flashId = common::request('flashId', 'G', 0);
        if (!$flashId || !is_numeric($flashId)) {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['is_open'] = 0;

        $info = $this->load('flashBuy')->getFlashIsStart($flashId);
        $isDisplay = 1;
        if ($info && $info['status'] <> 0) {
            $isDisplay = 0;
        }
        $isDisplay=0;//关闭提醒功能
        $this->result['result']['is_display'] = $isDisplay;//是否显示提醒（开始前显示）
        $uid = $this->uid;
        if (!$uid) {
            exit;
        }
        if ($this->load('flashBuy')->getRemindStatus($uid, $flashId)) {
            $this->result['result']['is_open'] = 1;
        }
    }

    /**
     * 闪购商品详情
     */
    public function dogetFlashDetail() {
        $uid = $this->uid;
        $flashId = common::request('flashId', 'R', 0);
        if (!$flashId || !is_numeric($flashId)) {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $info = $this->load('flashBuy')->getFlashBuyInfo($flashId);
        if (!$info) {
            $this->result['result']['list'] = [];
            exit;
        }
        $isRemind = 0;
        if ($uid) {
            if ($this->load('flashBuy')->getRemindStatus($uid, $flashId)) {
                $isRemind = 1;
            }
        }
        $childGoods = $this->load('goods')->getFlashGoodsList($info['id']);
        foreach($childGoods as $key=>$goods){
            $arrAttr = $this->load('goods')->getAllAttrByPid($goods['product']['id']);
            sort($arrAttr);
            $childGoods[$key]['attrs'] = $arrAttr;
        }
        $result = array(
            'id'=>$info['id'],
            'title'=>$info['title'],
            'imageUrl'=>$info['image'],
            'imageContent'=>$info['image_content'],
            'optionContent'=>$info['option_content'],
            'isRemind'=>$isRemind,
            'costPrice'=>$info['cost_price'],
            'startTime'=>$info['start_time'],
            'endTime'=>$info['end_time'],
            'currentTime'=>time(),
            'totalNum'=>$info['total_num'],
            'sale_num'=>$info['sale_num'],
            'goodList'=>$childGoods,
            'status'=>$info['status']
        );
        $this->result['result']['list'] = $result;
    }


}
